package com.home.fansea.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.alibaba.fastjson2.JSON;
import com.home.fansea.constant.RedisConstants;
import com.home.fansea.entity.User;
import com.home.fansea.service.TokenService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/**
 * Token工具类
 *
 * @author liaodezhi
 * @date 2023/07/04
 */
@Service
public class TokenServiceImpl implements TokenService {

    @Value("${jwt.secret}")
    private String secret;

    private final static String LOGIN_USER_KEY = "login_user_key";

    /**
     * 登录有效时间: 30分钟
     */
    private final static int EXPIRE_TIME = 30;

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public String createToken(User user) {
        user.setPassword(null);
//        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        redisTemplate.opsForValue().set(RedisConstants.USER_LOGIN_KEY + user.getId(), user, EXPIRE_TIME, TimeUnit.MINUTES);
//        redisTemplate.setExpireCache(RedisConstants.USER_LOGIN_KEY + user.getId(), user, EXPIRE_TIME, TimeUnit.MINUTES);
        HashMap<String, Object> playLoad = new HashMap<String, Object>() {
            {
                put(LOGIN_USER_KEY, user.getId());
            }
        };
        return JWTUtil.createToken(playLoad, secret.getBytes());
    }

    @Override
    public User getLoginUser(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (StrUtil.isEmpty(token) || StrUtil.isBlank(token)) {
            return null;
        }
        Integer id = getLoginKey(token);
        if (id == null){
            return null;
        }
//        return redisCache.getCache(RedisConstants.USER_LOGIN_KEY + id);
        String string = redisTemplate.opsForValue().get(RedisConstants.USER_LOGIN_KEY + id).toString();
        System.out.println("USER_LOGIN_KEY = " + string);
        return JSON.parseObject(string,User.class);
    }

    @Override
    public void refreshToken(User user) {
        redisTemplate.opsForValue().set(RedisConstants.USER_LOGIN_KEY + user.getId(), user, EXPIRE_TIME, TimeUnit.MINUTES);
//        redisCache.setExpireCache(RedisConstants.USER_LOGIN_KEY +  user.getId(), user, EXPIRE_TIME, TimeUnit.MINUTES);
    }

    //获取id用来拼接key
    private Integer getLoginKey(String token) {
        JWT jwt = JWTUtil.parseToken(token);
        return (Integer) jwt.getPayload(LOGIN_USER_KEY);
    }
    @Override
    public void removeToken(String token){
        Integer uuid = getLoginKey(token);
//        redisCache.deleteCache(RedisConstants.USER_LOGIN_KEY + uuid);
        redisTemplate.delete(RedisConstants.USER_LOGIN_KEY + uuid);
    }
}
